﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PredatorPrey.Library;

namespace PredatorPrey
{
	// Id		: $Id: Program.cs 39 2009-04-23 21:38:07Z kristof@kristofmattei.be $
	// Date		: $Date: 2009-04-23 21:38:07 +0000 (Thu, 23 Apr 2009) $
	// Author	: $Author: kristof@kristofmattei.be $
	// Revision	: $Revision: 39 $
	/// <summary>
	/// Entrypoint class of the program
	/// </summary>
	class Program
	{
		/// <summary>
		/// Main entrypoint
		/// </summary>
		/// <param name="args">Not used</param>
		static void Main(string[] args)
		{
			//hit the gas!
			(new Program()).Start();
		}

		/// <summary>
		/// Bootstrapper, start here
		/// </summary>
		private void Start()
		{
			List<string> lines = new List<string>();

			//read the file
			//we need to change this to a filedialog
			System.IO.StreamReader file = new System.IO.StreamReader(@"c:\Field.txt");

			string line = string.Empty;

			while ((line = file.ReadLine()) != null)
			{
				lines.Add(line);
			}

			//we now have the grid in a very stupid way.
			//convert it and pass it on
			Search startup = new Search(this.ConvertToField(lines));
			startup.StartSearch();
		}

		/// <summary>
		/// Stupid method to display the Field
		/// </summary>
		/// <param name="Field">An initialized Field</param>
		private void Visualize(Field field)
		{
			if (field == null)
			{
				throw new ArgumentNullException("Field cannot be null");
			}

			string output = string.Empty;

			for (int y = 0; y < field.Height; y++)
			{
				for (int x = 0; x < field.Width; x++)
				{
					output += string.Format("{0,-7}", field[x, field.Height - 1 - y].FieldType);
				}

				output += "\n";
			}

			System.Console.WriteLine(output);
			System.Console.ReadLine();
		}

		/// <summary>
		/// Convert a given set of lines to a Field
		/// </summary>
		/// <param name="lines">The lines from a text file</param>
		/// <returns>a Field</returns>
		private Field ConvertToField(List<string> lines)
		{
			if (lines == null)
			{
				throw new ArgumentNullException("Lines cannot be null");
			}

			//get the width
			int width = lines[0].Length;

			//get the height
			int height = lines.Count;

			Field field = new Field(width, height);

			for (int x = 0; x < width; x++)
			{
				for (int y = 0; y < height; y++)
				{
					field[x, y] = new Cell(x, y, this.ConvertCharToFieldType(lines[height - y - 1][x]));
				}
			}

			this.Visualize(field);

			return field;
		}

		/// <summary>
		/// Converts a char (from a file) to an FieldType
		/// </summary>
		/// <param name="c">The char</param>
		/// <returns>An equivilent FieldType for the char</returns>
		private FieldType ConvertCharToFieldType(char c)
		{
			FieldType fieldType;

			switch (c)
			{
				case 'W':
					fieldType = FieldType.Wall;
					break;
				case 'G':
					fieldType = FieldType.Grass;
					break;
				case 'E':
					fieldType = FieldType.Empty;
					break;
				case 'S':
					fieldType = FieldType.Swamp;
					break;
				case 'Y':
					fieldType = FieldType.Prey;
					break;
				case 'P':
					fieldType = FieldType.Predator;
					break;
				default:
					throw new ArgumentOutOfRangeException("char is not in accepted range");
			}

			return fieldType;
		}
	}
}
